package com.xbongbong.saas.service.impl;/*/*/

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.common.pojo.dto.LocationGetDTO;
import com.xbongbong.pro.common.pojo.dto.LocationSetDTO;
import com.xbongbong.pro.common.pojo.vo.LocationGetVO;
import com.xbongbong.pro.common.pojo.vo.LocationSetVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.domain.entity.WorkOrderEntity;
import com.xbongbong.saas.domain.entity.ext.ClueEntityExt;
import com.xbongbong.saas.domain.entity.ext.CustomerEntityExt;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.WorkOrderEnum;
import com.xbongbong.saas.help.SaasGetHelp;
import com.xbongbong.saas.model.ClueModel;
import com.xbongbong.saas.model.CustomerModel;
import com.xbongbong.saas.model.WorkOrderModel;
import com.xbongbong.saas.service.LocationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2019/5/13 11:17
 * @since v1.0
 * @version v1.0
 */
@Service("locationService")
public class LocationServiceImpl implements LocationService {
    private static final Logger LOG = LoggerFactory.getLogger(LocationServiceImpl.class);

    @Resource
    private SaasGetHelp saasGetHelp;
    @Resource
    private WorkOrderModel workOrderModel;
    @Resource
    private CustomerModel customerModel;
    @Resource
    private ClueModel clueModel;


    @Override
    public LocationGetVO getLocation(LocationGetDTO locationGetDTO) throws XbbException {
        LocationGetVO locationGetVO = new LocationGetVO();
        try {
            Integer businessType = locationGetDTO.getBusinessType();
            XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);
            if (Objects.isNull(xbbRefTypeEnum)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018);
            }
            Long dataId = locationGetDTO.getDataId();
            String corpid = locationGetDTO.getCorpid();

            PaasFormDataEntityExt paasFormDataEntityExt = saasGetHelp.getFormData(dataId, corpid, businessType);
            if (Objects.isNull(paasFormDataEntityExt)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018);
            }

            JSONObject data = paasFormDataEntityExt.getData();
            JSONObject addressObj = new JSONObject();
            String title = "";
            switch (xbbRefTypeEnum) {
                case WORK_ORDER:
                    addressObj = saasGetHelp.getJsonObjectFromFormData(data, WorkOrderEnum.ADDRESS.getAttr());
                    // TODO 工单名称
                    title = paasFormDataEntityExt.getSerialNo();
                    break;
                case CUSTOMER_MANAGEMENT:
                    addressObj = saasGetHelp.getJsonObjectFromFormData(data, CustomerManagementEnum.ADDRESS.getAttr());
                    title = saasGetHelp.getStringFromFormData(data, CustomerManagementEnum.NAME.getAttr());
                    break;
                case CLUE:
                    addressObj = saasGetHelp.getJsonObjectFromFormData(data, ClueEnum.ADDRESS.getAttr());
                    title = saasGetHelp.getStringFromFormData(data, ClueEnum.COMPANY_NAME.getAttr());
                    break;
                default:
                    break;
            }

            if (Objects.nonNull(addressObj)) {
                JSONObject locationObj = saasGetHelp.getJsonObjectFromFormData(addressObj, "location");
                if (Objects.nonNull(locationObj)) {
                    locationGetVO.setLat(locationObj.getDouble("lat") != null ? locationObj.getDouble("lat") : 0D);
                    locationGetVO.setLon(locationObj.getDouble("lon") != null ? locationObj.getDouble("lon") : 0D);
                }
            }
            locationGetVO.setTitle(title);


        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("LocationServiceImpl.getLocation 出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return locationGetVO;
    }


    @Override
    public LocationSetVO setLocation(LocationSetDTO locationSetDTO) throws XbbException {
        LocationSetVO locationSetVO = new LocationSetVO();
        try {
            Integer businessType = locationSetDTO.getBusinessType();
            XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);
            if (Objects.isNull(xbbRefTypeEnum)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018);
            }
            Long dataId = locationSetDTO.getDataId();
            String corpid = locationSetDTO.getCorpid();

            if (Objects.equals(xbbRefTypeEnum, XbbRefTypeEnum.WORK_ORDER)) {
                //工单
                WorkOrderEntity workOrderEntity = workOrderModel.getByKey(dataId, corpid);
                if (Objects.isNull(workOrderEntity)) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018);
                }
                JSONObject data = workOrderEntity.getData();
                data.put(WorkOrderEnum.ADDRESS.getAttr(), locationSetDTO.getAddress());
                workOrderEntity.setData(data);
                workOrderModel.update(workOrderEntity);
            } else if (Objects.equals(xbbRefTypeEnum, XbbRefTypeEnum.CUSTOMER_MANAGEMENT)){
                PaasFormDataEntityExt paasFormDataEntity = customerModel.getByKey(dataId, corpid);
                if (Objects.isNull(paasFormDataEntity)) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018);
                }
                JSONObject data = paasFormDataEntity.getData();
                data.put(CustomerManagementEnum.ADDRESS.getAttr(), locationSetDTO.getAddress());
                paasFormDataEntity.setData(data);
                CustomerEntityExt customerEntityExt = new CustomerEntityExt();
                BeanUtil.copyProperties(paasFormDataEntity, customerEntityExt);
                if (Objects.nonNull(customerEntityExt.getData()) && !customerEntityExt.getData().containsKey(CustomerManagementEnum.NAME.getAttr())) {
                    LOG.info("locationServiceImpl.save not have customerName"+ JSON.toJSONString(customerEntityExt.getData()));
                }
                customerModel.update(customerEntityExt);
            } else if (Objects.equals(xbbRefTypeEnum, XbbRefTypeEnum.CLUE)){
                PaasFormDataEntityExt paasFormDataEntity = clueModel.getByKey(dataId, corpid);
                if (Objects.isNull(paasFormDataEntity)) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018);
                }
                JSONObject data = paasFormDataEntity.getData();
                data.put(ClueEnum.ADDRESS.getAttr(), locationSetDTO.getAddress());
                paasFormDataEntity.setData(data);
                ClueEntityExt clueEntityExt = new ClueEntityExt();
                BeanUtil.copyProperties(paasFormDataEntity, clueEntityExt);
                if (Objects.nonNull(clueEntityExt.getData()) && !clueEntityExt.getData().containsKey(ClueEnum.COMPANY_NAME.getAttr())) {
                    LOG.info("locationServiceImpl.save not have clueName"+ JSON.toJSONString(clueEntityExt.getData()));
                }
                clueModel.update(clueEntityExt);
            }
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("LocationServiceImpl.setLocation 出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return locationSetVO;
    }


}
